"""
@Project:workflow_python
@File:serializers.py
@Author:袁浩天
@Date:9:11
"""

from .models import *

from rest_framework import serializers
from rest_framework.validators import ValidationError

import re


# 职位的序列化器name rank department position_time
class PositionSerTow(serializers.ModelSerializer):
    class Meta:
        model = PositionModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 职位名称由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 员工序列化器
class UserSer(serializers.ModelSerializer):
    password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)

    class Meta:
        model = UserModel
        fields = "__all__"

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")
        mobile = attrs.get("mobile")

        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", name):
            raise ValidationError(f'{name}姓名由2到9为汉字组成')

        if not re.findall(r"^1[3-9]\d{9}$", mobile):
            raise ValidationError(f"{mobile}格式不正确")

        return attrs


# 职位的序列化器name rank department position_time
class PositionSer(serializers.ModelSerializer):
    rank = serializers.PrimaryKeyRelatedField(queryset=RankModel.objects.all())
    rankname = serializers.SerializerMethodField()

    department = serializers.PrimaryKeyRelatedField(queryset=DepartmentModel.objects.all())
    departmentname = serializers.SerializerMethodField()

    def get_rankname(self, obj):
        return obj.rank.name

    def get_departmentname(self, obj):
        return obj.department.name

    class Meta:
        model = PositionModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")
        rank_id = attrs.get("rank")
        department_id = attrs.get("department")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 职位名称由2到30位汉字、数字、字母或下划线组成！')

        rank_list = RankModel.objects.filter(is_delete=False).values("id")
        rank_list = [i.get("id") for i in rank_list]
        if rank_id not in rank_list:
            raise ValidationError(f"{rank_id} 该职级不存在！")

        staff_list = DepartmentModel.objects.filter(is_delete=False).values("id")
        staff_list = [i.get("id") for i in staff_list]
        if department_id not in staff_list:
            raise ValidationError(f"{department_id} 该部门不存在！")

        return attrs


# 部门的序列化器
class DepartmentSer(serializers.ModelSerializer):
    class Meta:
        model = DepartmentModel
        fields = "__all__"

    def validate(self,attrs):
        name = attrs.get("name")
        if not re.findall(r"[\u4e00-\u9fa5]{1,9}",name):
            raise ValidationError(f'{name}名称由2到9为汉字组成')
        return attrs


# 职级的序列化器
class RankSer(serializers.ModelSerializer):
    class Meta:
        model = RankModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")
        if not re.findall(r"^[A-Za-z]{1,5}\d{1,5}$",name):
            raise ValidationError(f'{name} 职级名称由字母加数字组成！')
        return attrs


# 职位角色中间表中间表序列化器
class PositionRoleSer(serializers.ModelSerializer):
    position = serializers.PrimaryKeyRelatedField(queryset=PositionModel.objects.all())
    positionname = serializers.SerializerMethodField()

    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    rolename = serializers.SerializerMethodField()

    def get_positionname(self, obj):
        return obj.position.name

    def get_rolename(self, obj):
        return obj.role.rolename

    class Meta:
        model = PositionRoleModel
        fields = "__all__"


# 菜单管理
class MenuSer(serializers.ModelSerializer):
    class Meta:
        model = MenuModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")
        if not re.findall(r"[\u4e00-\u9fa5]{1,9}",name):
            raise ValidationError(f'{name}名称由2到9为汉字组成')
        return attrs


# 角色管理
class RoleSer(serializers.ModelSerializer):
    class Meta:
        model = RoleModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("rolename")
        if not re.findall(r"[\u4e00-\u9fa5]{1,9}",name):
            raise ValidationError(f'{name}名称由2到9为汉字组成')
        return attrs


# 权限的序列化器
class PermissionSer(serializers.ModelSerializer):
    class Meta:
        model = PermissionModel
        fields = "__all__"



